package com.zzyl.member.service.impl;

import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.member.domain.Reservation;
import com.zzyl.member.dto.servceListDTO;
import com.zzyl.member.mapper.ReservationMapper;
import com.zzyl.member.service.IReservationService;
import com.zzyl.member.vo.TimeReservationCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.zzyl.framework.interceptor.impl.UserThreadLocal.getUserId;

/**
 * 预约信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Integer id) {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param
     * @return 预约信息
     */
    @Override
    public IPage<Reservation> selectReservationList(servceListDTO servceListDTO) {
        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<Reservation>()
                .eq(ObjectUtil.isNotNull(servceListDTO.getStatus()), Reservation::getStatus, servceListDTO.getStatus());

        return page(Page.of(servceListDTO.getPageNum(), servceListDTO.getPageSize()), wrapper);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        // 设置默认的 status 值，例如 0 表示未处理
        if (reservation.getStatus() == null) {
            reservation.setStatus(0);
        }
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public void updateReservation(Integer reservation) {
        Reservation one = lambdaQuery().eq(Reservation::getId, reservation).one();
        one.setStatus(2);
        updateById(one);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Integer[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Integer id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public Long getCancelledCount() {
        //获取当天开始时间和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        //从本地拿到登陆人的id
        Long userId = getUserId();
        //查询当天符合要求的预约次数
        Long count = lambdaQuery()
                .eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, userId)
                .between(Reservation::getUpdateTime, startTime, endTime)
                .count();

        if (count > 3) {
            throw new BaseException("预约次数已达上限");
        }
        return count;
    }

    /**
     * 各个时间段的预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<TimeReservationCountVO> getReservationCountByTime(Long time) {
        //解析time
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        List<TimeReservationCountVO> list = reservationMapper.getReservationCountByTime(startTime, endTime);

        return list;

    }

}
